﻿using UnityEngine;
using System.Collections;

public class GenericButton : Actors
{
	private bool m_IsMouseOver = false;
	private bool m_IsClicking = false;
	private bool m_OnMouseClick = false;
	private SpriteRenderer m_ButtonRenderer;
	private TextMesh m_ButtonText;
	private Color m_ButtonInitialColor;
	private Color m_TextInitialColor;
	private Vector3 m_InitialPosition;
	private Vector3 m_ClickedNewPosition;

	public bool OnMouseClick {get { return m_OnMouseClick;}}

	void Start () 
	{
		m_ButtonRenderer = aTransform.GetComponent<SpriteRenderer>();
		m_ButtonText = aTransform.GetChildByName("ButtonText").GetComponent<TextMesh>();

		m_ButtonInitialColor = m_ButtonRenderer.color;
		m_TextInitialColor = m_ButtonText.color;
		m_InitialPosition = aTransform.position;
		m_ClickedNewPosition = new Vector3(m_InitialPosition.x + 0.05f, m_InitialPosition.y - 0.05f, m_InitialPosition.z);
	}

	void Update () 
	{
		if(m_OnMouseClick)
		{
			Debug.Log("ONMOUSECLICK");
			m_OnMouseClick = false;
		}


		if(m_IsMouseOver)
		{
			if(Input.GetMouseButton(0))
			{
				m_IsClicking = true;
			}
			else if(m_IsClicking && Input.GetMouseButtonUp(0))
			{
				m_IsClicking = false;
				m_OnMouseClick = true;
			}
		}
		else
		{
			m_IsClicking = false;
		}
		
		if(Input.GetMouseButtonUp(0))
		{
			m_IsClicking = false;
		}
		if(m_IsClicking)
		{
			aTransform.position = m_ClickedNewPosition;
		}
		else
		{
			aTransform.position = m_InitialPosition;
		}
	}
	
	private void OnMouseEnter()
	{
		if(!Input.GetMouseButton(0))
		{
			m_IsMouseOver = true;
			m_ButtonRenderer.color = Color.white;
			m_ButtonText.color = Color.white;
		}
	}
	
	private void OnMouseExit()
	{
		m_IsMouseOver = false;
		m_ButtonRenderer.color = m_ButtonInitialColor;
		m_ButtonText.color = m_TextInitialColor;
	}
}
